package br.com.dreamsource.mobile.worders.forms.customer;

import br.com.dreamsource.mobile.jmesql.exceptions.SQLException;
import br.com.dreamsource.mobile.worders.WOrders;
import br.com.dreamsource.mobile.components.FormWOrders;
import br.com.dreamsource.mobile.worders.forms.core.Consts;
import br.com.dreamsource.mobile.worders.forms.core.DatabaseManager;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.TextField;

/**        
 *
 * @author Elizeu Nogueira da Rosa Jr.
 * @version 0.1
 * @date 17.05.2007
 *
 */

public class Customer extends FormWOrders {
    
    private TextField tfLocalizar;
    
    private ChoiceGroup radioGroupLocateBy;
    private String[] radioGroupLocateByItems;
    private Image[] radioGroupImage;
    
    public Customer(CommandListener listener, WOrders midlet) {
        super(listener, midlet, "Clientes");
    }

    private ChoiceGroup getRadioGroupLocateBy() {
        if (radioGroupLocateBy == null) {
            
            radioGroupLocateByItems = new String[]{"Nome", "Cpf"};
            radioGroupImage = new Image[]{null, null};
            
            radioGroupLocateBy = new ChoiceGroup("Localizar", Choice.EXCLUSIVE, 
                    radioGroupLocateByItems, radioGroupImage);
            radioGroupLocateBy.setSelectedFlags(new boolean[]{true, false});
        }
        return radioGroupLocateBy;
    }    
    
    private TextField getTfLocalizar() {
        if (tfLocalizar == null) {
            tfLocalizar = new TextField("Localizar", "", 80, 0x0);
        }
        return tfLocalizar;
    }        
    
    protected void createComponents() {
        this.append(this.getTfLocalizar());
        this.append(this.getRadioGroupLocateBy());
        this.addCommand(this.getLocateCommand());
        this.addCommand(this.getBackCommand());
        this.addCommand(this.getInsertCommand());
        
    }
    
    public void fireCommand(Command command) {
        if (command == locateCommand) {
            if (radioGroupLocateBy.getSelectedIndex() == 0) {
                String searchString = this.tfLocalizar.getString();
                if (searchString.length() > 5) {
                    if (radioGroupLocateBy.getSelectedIndex() == 0) {
                        try {
                            DatabaseManager dbManager = new DatabaseManager(this.midlet.getUserPOJO());
                            this.sendToForm(this.midlet.getCustomerList(
                                    dbManager.findByFieldLike(
                                        Consts.CUSTOMER_LOCATE_BY_FIELD,
                                        Consts.CUSTOMER_NAME,
                                        this.tfLocalizar.getString())
                                        )
                                    );
                        } catch (SQLException sqlEx) {
                            this.sendToForm(this.midlet.getSystemMessage("ERRO", sqlEx.getMessage(), this));
                        }
                    } else {
                        if (radioGroupLocateBy.getSelectedIndex() == 1) {
                            this.sendToForm(this.midlet.getOrderList(null));
                        }
                    }
                } else {
                    this.sendToForm(this.midlet.getSystemMessage("Information", 
                            "Voce deve digitar ao menos  caracteres!", this)
                            );
                }
            }
        } else 
        if (command == backCommand) {
            this.sendToForm(this.midlet.getMainMenu());
            this.midlet.destroyForm(this);
        } else 
        if (command == insertCommand) {
            this.sendToForm(this.midlet.getCustomerEdit(Consts.UPDATE_TYPE_INSERT, null));
            this.midlet.destroyForm(this);
        }
    }

}
